/**
 * 
 */
package jumpingnotes.util;
import java.util.Properties; 
import javax.mail.*; 
import javax.mail.internet.*;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MailHelper {

	private String smtphost;
	private String user;
	private String password;
	public MailHelper()
	{}
	public String getSmtphost() {
		return smtphost;
	}
	public void setSmtphost(String smtphost) {
		this.smtphost = smtphost;
	}
	public String getUser() {
		return user;
	}
	public void setUser(String user) {
		this.user = user;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public boolean sendEmail(String email, String title, String body)
	{
		boolean retVal = true;
		try {
			//Get system properties 
			Properties props=System.getProperties(); 
			props.put("mail.transport.protocol", "smtp");
			//Setup mail server 
			props.put("mail.smtp.host",smtphost); 
			props.put("mail.smtp.auth", "true");
			props.put("mail.smtp.starttls.enable", "true"); 
			//Get Session 
			Session session =Session.getInstance(props,null); 
			//session.setDebug(true);
			//Define message 
			MimeMessage message=new MimeMessage(session); 
			message.setFrom(new InternetAddress(user)); 
			message.addRecipient(Message.RecipientType.TO, new InternetAddress(email)); 
			message.setSubject(title); 
			message.setText(body);  
			//send message 
			Transport transport=session.getTransport("smtp");
			transport.connect(smtphost,user,password);
			transport.sendMessage(message,message.getAllRecipients());
			transport.close();

			//Transport.send(message);
		} catch (AddressException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			retVal = false;
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			retVal = false;
		} 
		return retVal;
	}
	
}
